﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using AnalisisDeRiesgos.Dal;

namespace AnalisisDeRiesgos.Models.Action
{
    public class EditActionModel
    {
        #region Propierties
        public int ActionId { get; set; }
        public int DangerId { get; set; }

        [DisplayName("Nombre de la Actividad")]
        [Required(ErrorMessage = "El tipo de actividad es necesario")]
        public int Type { get; set; }

        [DisplayName("Accion")]
        [Required(ErrorMessage = "La accion es requerida")]
        [StringLength(100, ErrorMessage = "Toda la accion debe tener como maximo 100 caracteres")]
        public string DescriptionAction { get; set; }

        [DisplayName("Responsable")]
        public int Responsible { get; set; }

        public string ResponsibleName { get; set; }

        [DisplayName("Fecha programada")]
        public string ProgrammedDate { get; set; }

        [DisplayName("Fecha de ejecucion")]
        public string ExecutionDate { get; set; }

        [DisplayName("Estado (%)")]
        [StringLength(3, ErrorMessage = "El estado solo puede ser 3 cifras")]
        [RegularExpression(@"^[0-9.,]+$", ErrorMessage = "El estado solo puede ser un numero")]
        public string Status { get; set; }
        #endregion

        public IEnumerable<SelectListItem> ActionTypes
        {
            get
            {
                return actionList;
            }
        }

        private List<SelectListItem> actionList;

        riskanalysisEntities entities = new riskanalysisEntities();

        public EditActionModel()
        {
            DangerId = 1;
            FillList();
        }

        public EditActionModel(int actionId)
        {
            FillActionById(actionId);            
        }

        private void FillList()
        {
            actionList = Utils.FillAvailableActionTypesList(DangerId);
        }

        private void FillActionById(int id)
        {
            action theAction = GetActionById(id);

            ActionId = theAction.actionId;
            DangerId = theAction.dangerId;
            FillList();
            Type = theAction.type;
            DescriptionAction = theAction.description;
            Responsible = theAction.responsible;
            ResponsibleName = theAction.user.firstName + " " + theAction.user.lastName + " " + theAction.user.secondLastName;
            ProgrammedDate = theAction.programedDate.ToString("d", Utils.culture);
            ExecutionDate = theAction.executionDate.ToString("d", Utils.culture);
            Status = theAction.status.ToString();
        }

        private action GetActionById(int id)
        {
            var query = from a in entities.actions
                        where a.actionId == id
                        select a;
            action theAction = query.FirstOrDefault();

            if (theAction == null)
                throw new EditActionException("La accion especificada no existe");
            return theAction;
        }

        public void Save()
        {
            action theAction = GetActionById(ActionId);

            theAction.description = DescriptionAction;
            theAction.executionDate = Convert.ToDateTime(ExecutionDate, Utils.culture);
            theAction.programedDate = Convert.ToDateTime(ProgrammedDate, Utils.culture);
            theAction.responsible = Responsible;
            theAction.status = Convert.ToInt32(Status);
            theAction.type = Type;
            entities.SaveChanges();
        }
    }
}